//
//  ScanResultVC.m
//  AILock
//
//  Created by 谭亚明 on 2020/7/6.
//  Copyright © 2020 tym. All rights reserved.
//

#import "ScanResultVC.h"
#import "RoomSeleView.h"
#import "EquipmentNameVC.h"
#import "EquipmentService.h"
#import "UIButton+Gradient.h"
#import "FamilyRoomService.h"
@interface ScanResultVC ()

@property(nonatomic,weak) IBOutlet UIButton *configBtn;

@property(nonatomic,weak) IBOutlet UILabel *nameLab;
@property(nonatomic,weak) IBOutlet UILabel  *roomNameLab;

@property(nonatomic,copy) EquipmentService *services;
@property(nonatomic,copy) FamilyRoomService *roomService;
@property(nonatomic,copy) NSMutableArray *roomsArray;
@property(nonatomic,strong) EquipmentTypeModel *typeModel;
@end

@implementation ScanResultVC

-(instancetype)initWithEquipmentMode:(EquipmentTypeModel*)model{
    self = [super init];
    if (self) {
        _typeModel = model;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addBackBtnFunc];
    [self addTitle:@"扫描结果"];
    
    self.configBtn.layer.cornerRadius = 5;
    self.configBtn.layer.masksToBounds = YES;
    [self.configBtn gradientButtonWithSize:CGSizeMake(300, 44) colorArray:@[(id)rgba(139, 175, 255),(id)rgba(76, 132, 255)] percentageArray:@[@(0.5),@(1)] gradientType:GradientFromLeftTopToRightBottom];
    self.tz_interactivePopDisabled = YES;
    if (self.typeModel.typeName.length) {
        self.nameLab.text = self.typeModel.typeName;
    }
    
    [self getAllRooms];
    [self getEquipmentDetail];
}

-(void)backViewFunc:(id)btn{
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}

-(EquipmentService*)services{
    if (_services == nil) {
        _services = [[EquipmentService alloc] init];
    }
    return _services;
}

-(FamilyRoomService*)roomService{
    if (_roomService == nil) {
        _roomService = [[FamilyRoomService alloc] init];
    }
    return _roomService;
}

-(NSMutableArray*)roomsArray{
    if (_roomsArray == nil) {
        _roomsArray = [[NSMutableArray alloc] init];
    }
    return _roomsArray;
}

-(void)getAllRooms{
    TYMWeak(self);
    [self.roomService getFindfamilyroomfamilyId:[UserTool getCurrentFamily].familyId completion:^(LMJBaseResponse * _Nonnull response, NSArray * _Nonnull roosArry) {
        if (response.message) {
            [WHToast showMessage:response.message duration:2 finishHandler:nil];
        }else{
            [weakself.roomsArray addObjectsFromArray:roosArry];
        }
    }];
}

-(void)getEquipmentDetail{
    TYMWeak(self);
    [self.services getEquipmentinfo:self.typeModel.equipmentId completion:^(NSError * _Nonnull error, EquipmentModel * _Nonnull equipmentModel) {
        weakself.roomNameLab.text = equipmentModel.roomName;
        weakself.nameLab.text = equipmentModel.equipmentName;
    }];
}

//选择房间
-(IBAction)selectedRoomAction:(id)sender{
    
    NSMutableArray *roomNames = [[NSMutableArray alloc] init];
    NSInteger index = 0;
    for (int i=0;i<self.roomsArray.count;i++) {
        RoomModel *model = self.roomsArray[i];
        if ([model.roomName isEqualToString:self.roomNameLab.text]) {
            index = i;
        }
        [roomNames addObject:model.roomName];
    }
    RoomSeleView *pop = [[RoomSeleView alloc]initWithFrame:self.view.frame];
    pop.data = roomNames;
    pop.index = @(index);
    TYMWeak(self);
    
    pop.seleRoomBackIndex = ^(NSInteger index) {
        [weakself setEquipmentWithRoomIndex:index];
    };
    [pop viewShow];
}

//修改姓名
-(IBAction)changeNameAction:(id)sender{

    EquipmentNameVC *equipVC = [[EquipmentNameVC alloc] init];
    equipVC.equipMentID = self.typeModel.equipmentId;
    equipVC.nameStr = self.nameLab.text;
    
    TYMWeak(self);
    equipVC.equipmentNameBlock = ^(NSString * _Nonnull equipName) {
        weakself.nameLab.text = equipName;
    };
    [self.navigationController pushViewController:equipVC animated:YES];
}

-(void)setEquipmentWithRoomIndex:(NSInteger)index{
    RoomModel *roomModel = self.roomsArray[index];
    [self.services getMoveEquipment:@[@(self.typeModel.equipmentId)] familyRoomId:roomModel.familyRoomId?roomModel.familyRoomId:roomModel.id completion:^(LMJBaseResponse * _Nonnull response) {
        if (response.message) {
            [WHToast showMessage:response.message duration:2 finishHandler:nil];
        }else{
            self.roomNameLab.text = roomModel.roomName;
        }
    }];
}

-(IBAction)soureAction:(id)sender{

    [self.navigationController popToRootViewControllerAnimated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
